package com.easykoo.web.tag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import java.io.IOException;

public class ColumnTag extends BaseTag {
    private String orderBy;
    private String style;
    private String styleClass;
    private String background;
    private String width;
    private String height;
    private String id;
    private String bgcolor;
    private String font;

    private JspWriter out;
    private FormTag formTag;


    public int doStartTag() throws JspException {
        formTag = (FormTag) getParent();

        StringBuffer body = new StringBuffer("");
        body.append("<td ");
        appendAttr(body);

        if (isNotBlank(orderBy)) {
            body.append("onclick=\"window.location.href = \'" + formTag.getUrl());
            body.append("?orderBy=" + orderBy);
            if (orderBy.equals(formTag.getPageInfo().getOrderBy()) && !formTag.getPageInfo().isAsc()) {
                body.append("&asc=1");
            } else {
                body.append("&asc=0");
            }
            body.append("\'\"");
            body.append("style=\"cursor:hand\"");
        }
        body.append(">");

        out = this.pageContext.getOut();
        try {
            out.write(body.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return EVAL_BODY_INCLUDE;
    }

    @Override
    public int doEndTag() throws JspException {
        try {

            StringBuffer body = new StringBuffer("&nbsp;&nbsp;");
            if (isNotBlank(orderBy)) {
                if (orderBy.equals(formTag.getPageInfo().getOrderBy()) && !formTag.getPageInfo().isAsc()) {
                    body.append("▼");
                } else {
                    body.append("▲");
                }
            }
            body.append("</td>");

            out.write(body.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return EVAL_PAGE;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    public String getStyle() {
        return style;
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public String getStyleClass() {
        return styleClass;
    }

    public void setStyleClass(String styleClass) {
        this.styleClass = styleClass;
    }

    public String getBackground() {
        return background;
    }

    public void setBackground(String background) {
        if (background.contains("/")) {
            if (background.startsWith("/")) {
                this.background = ((HttpServletRequest) pageContext.getRequest()).getContextPath() + background;
            } else {
                this.background = ((HttpServletRequest) pageContext.getRequest()).getContextPath() + "/" + background;
            }
        } else {
            this.background = background;
        }
    }


    public String getWidth() {
        return width;
    }

    public void setWidth(String width) {
        this.width = width;
    }

    public String getHeight() {
        return height;
    }

    public void setHeight(String height) {
        this.height = height;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getBgcolor() {
        return bgcolor;
    }

    public void setBgcolor(String bgcolor) {
        this.bgcolor = bgcolor;
    }

    public String getFont() {
        return font;
    }

    public void setFont(String font) {
        this.font = font;
    }
}